
#import "SMPostComposerViewController.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import <Masonry/Masonry.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "../../Managers/SMDataStore.h"
#import "../../Models/SMMoment.h"

@interface SMPostComposerViewController () <TZImagePickerControllerDelegate, UITextViewDelegate>
@property(nonatomic, strong) UITextView *tv;
@property(nonatomic, strong) UIButton *pickBtn;
@property(nonatomic, strong) UILabel *countL;
@property(nonatomic, strong) NSMutableArray<NSString *> *imageURLs;
@end

@implementation SMPostComposerViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发动态";
    self.view.backgroundColor = UIColor.systemBackgroundColor;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStyleDone target:self action:@selector(publish)];
    _tv = [UITextView new]; _tv.font=[UIFont systemFontOfSize:16]; _tv.layer.borderColor=UIColor.tertiaryLabelColor.CGColor; _tv.layer.borderWidth=1; _tv.layer.cornerRadius=8;
    _pickBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_pickBtn setTitle:@"选择图片(最多9张)" forState:UIControlStateNormal]; [_pickBtn addTarget:self action:@selector(pick) forControlEvents:UIControlEventTouchUpInside];
    _countL = [UILabel new]; _countL.text = @"已选 0 张";
    [self.view addSubview:_tv]; [self.view addSubview:_pickBtn]; [self.view addSubview:_countL];
    [_tv mas_makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(16); make.left.equalTo(self.view).offset(16); make.right.equalTo(self.view).offset(-16); make.height.mas_equalTo(160);}];
    [_pickBtn mas_makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(self->_tv.mas_bottom).offset(12); make.left.equalTo(self->_tv);}];
    [_countL mas_makeConstraints:^(MASConstraintMaker *make){ make.centerY.equalTo(self->_pickBtn); make.right.equalTo(self->_tv);}];
    _imageURLs = [NSMutableArray array];
}
- (void)pick {
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
    picker.allowTakePicture = YES; picker.allowPickingVideo = NO;
    [self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    // Demo: 用随机占位URL代替上传后的真实URL
    [_imageURLs removeAllObjects];
    for (NSInteger i=0;i<photos.count;i++){ [_imageURLs addObject:[NSString stringWithFormat:@"https://picsum.photos/200?%ld",(long)i]]; }
    _countL.text = [NSString stringWithFormat:@"已选 %ld 张",(long)_imageURLs.count];
}
- (void)publish {
    if (_tv.text.length == 0 && _imageURLs.count == 0) { [SVProgressHUD showErrorWithStatus:@"请填写内容或选择图片"]; return; }
    SMMoment *m = [SMMoment new]; m.momentId = [NSUUID UUID].UUIDString; m.author=@"我"; m.avatarURL=@"https://i.pravatar.cc/100?u=me"; m.text=_tv.text; m.imageURLs=_imageURLs.copy; m.createdAt=[NSDate date];
    [[SMDataStore shared] addMoment:m];
    [SVProgressHUD showSuccessWithStatus:@"发布成功"];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
